﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Data.Objects;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using FILSTARTUPS_CMS.Models;
using FILSTARTUPS_CMS.Helpers;

namespace FILSTARTUPS_CMS.Controllers
{
    [AdminAccess]
    public class PageController : Controller
    {
        FilStartUpsEntities cms_dbconnect = new FilStartUpsEntities();
        //
        // GET: /Page/
        
        [HttpPost]
        public JsonResult GetPageTitle(string query)
        {
            var page = (from p in cms_dbconnect.Pages
                        where p.Pagetitle.Contains(query)
                        orderby p.Pagetitle
                        select p.Pagetitle).Distinct().ToArray();

            return Json(page, JsonRequestBehavior.AllowGet);
        }
        public PartialViewResult SearchPage(string query)
        {
            var pages = (from p in cms_dbconnect.Pages
                         join r in cms_dbconnect.RoutingType
                         on p.routingidFK equals r.routingidPK
                         //  where p.routingidFK.Equals(2)
                         where p.Pagetitle.Equals(query)
                         orderby p.pageidPK descending
                         select new PageGrid
                         {
                             datecreated = p.datecreated,
                             pageidPK = p.pageidPK,
                             pagename = p.Pagetitle,
                             routing = r.routingname
                         }).ToList();
          return PartialView("_PageList", pages);
        }
        public ActionResult Index()
        {
            var pages = (from p in cms_dbconnect.Pages
                         join r in cms_dbconnect.RoutingType
                         on p.routingidFK equals r.routingidPK
                       //  where p.routingidFK.Equals(2)
                         orderby p.pageidPK descending
                         select new PageGrid
                         {
                             datecreated=p.datecreated,
                             pageidPK=p.pageidPK,
                             pagename=p.Pagetitle,
                             routing=r.routingname
                         }).ToList();

            return View(pages);
        }
        public ActionResult Create()
        {
            ViewData["routing"] = cms_dbconnect.RoutingType.OrderBy(r => r.routingidPK).ToList();
            return View();
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(Pages page, FormCollection form )
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (form["Pagedescription"].Trim() == string.Empty)
                    {
                        TempData["message"] = string.Format("Page Description is Empty");
                        ViewData["routing"] = cms_dbconnect.RoutingType.OrderBy(r => r.routingidPK).ToList();
                        return View(page);
                    }
                    else
                    {
                        
                        page.datecreated = DateTime.Now;
                        page.routingidFK = Convert.ToInt32(form["routingidFK"].Trim());
                        cms_dbconnect.Pages.Add(page);
                        cms_dbconnect.SaveChanges();
                        TempData["message"] = string.Format("{0} has been saved", page.Pagetitle);
                        return RedirectToAction("Index");
                    }
               }
            }
            catch
            {
                return View();
            }
            return View();
        }
        public ActionResult Edit(int id)
        {
            Pages page = cms_dbconnect.Pages.Find(id);
            ViewData["routing"] = cms_dbconnect.RoutingType.OrderBy(r => r.routingidPK).ToList();
            return View(page);
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(FormCollection form, Pages page)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    page.routingidFK = Convert.ToInt32(form["routingidFK"].Trim());
                    page.datecreated = page.datecreated;
                    cms_dbconnect.Entry(page).State = EntityState.Modified;
                    cms_dbconnect.SaveChanges();
                    TempData["message"] = string.Format("{0} has been modified", page.Pagetitle);
                    return RedirectToAction("Index");
                }
            }
            catch
            {

            }
            return View();
        }

        public ActionResult Details(int id)
        {
            var pages = (from p in cms_dbconnect.Pages
                         join r in cms_dbconnect.RoutingType
                         on p.routingidFK equals r.routingidPK
                         where p.pageidPK.Equals(id)
                         orderby p.pageidPK descending
                         select new PageGrid
                         {
                             datecreated = p.datecreated,
                             pageidPK = p.pageidPK,
                             pagename = p.Pagetitle,
                             pagedescription=p.Pagedescription,
                             routing = r.routingname
                         }).SingleOrDefault();

            return View(pages);
        }


    }
}
